package com.xunlai.infra.permission.manager.web.service.impl;

import com.xunlai.infra.permission.common.exception.DataNotExistException;
import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.manager.web.converter.PositionConverter;
import com.xunlai.infra.permission.manager.web.service.PositionService;
import com.xunlai.infra.permission.manager.web.vo.BasePageRespVo;
import com.xunlai.infra.permission.manager.web.vo.position.CreatePositionReqVo;
import com.xunlai.infra.permission.manager.web.vo.position.PositionListVo;
import com.xunlai.infra.permission.manager.web.vo.position.SearchPositionReqVo;
import com.xunlai.infra.permission.manager.web.vo.position.UpdatePositionReqVo;
import com.xunlai.infra.permission.storage.entity.Position;
import com.xunlai.infra.permission.storage.entity.Role;
import com.xunlai.infra.permission.storage.repository.PositionRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

import static com.xunlai.infra.permission.manager.web.converter.PositionConverter.INSTANCE;

/**
 * @author liang
 * @date 12/21/22 10:06 AM
 */
@Service
public class PositionServiceImpl implements PositionService {

    private PositionRepository positionRepository;

    @Override
    public Long createPosition(CreatePositionReqVo createPositionReqVo) throws PermissionBusinessException {
        Position position = INSTANCE.convertCreateReqVo2Position(createPositionReqVo);
        position.setEnable(Boolean.TRUE);
        Position save = positionRepository.save(position);
        return save.getId();
    }

    @Override
    public void updatePosition(UpdatePositionReqVo updatePositionReqVo) throws PermissionBusinessException {
        if(StringUtils.isBlank(updatePositionReqVo.getPositionName() ) && updatePositionReqVo.getEnable() == null){
            return;
        }
        Optional<Position> positionOptional = positionRepository.findById(updatePositionReqVo.getId());
        if(positionOptional.isEmpty()){
            throw new DataNotExistException("需要修改的岗位不存在");
        }
        Position position = positionOptional.get();
        if(StringUtils.isNotBlank(updatePositionReqVo.getPositionName())){
            position.setPositionName(updatePositionReqVo.getPositionName());
        }
        if(updatePositionReqVo.getEnable() != null){
            position.setEnable(updatePositionReqVo.getEnable());
        }
        positionRepository.save(position);
    }

    @Override
    public void deletePosition(Long id, boolean force) throws PermissionBusinessException {
        if(!force){
            //TODO 检查岗位下是否关联职工
        }
        positionRepository.deleteById(id);
    }

    @Override
    public BasePageRespVo<PositionListVo> pageListPosition(SearchPositionReqVo searchPositionReqVo, Integer pageNum, Integer pageSize) {
        Position position = INSTANCE.convertSearchReqVo2Position(searchPositionReqVo);
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        int size = (pageSize == null || pageSize <= 0) ? 10 : pageSize;
        Page<Position> positionPage = positionRepository.findAll(
                Example.of(position,
                        ExampleMatcher.matching()
                                .withIgnoreNullValues()
                                .withMatcher("positionName",(g) -> g.caseSensitive().contains())
                                .withMatcher("positionCode",g -> g.caseSensitive().exact())
                )
                , PageRequest.of(pageNum -1, size)
        );
        return new BasePageRespVo<>(positionPage.getContent().stream().map(INSTANCE::convert2ListVo).toList(),pageNum,pageSize,positionPage.getTotalElements(), positionPage.getTotalPages());
    }

    @Autowired
    public void setPositionRepository(PositionRepository positionRepository) {
        this.positionRepository = positionRepository;
    }
}
